﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Globalization;
using MobileWebsiteGenerator.Utility;
using MobileWebsiteGenerator.Models;
using MobileWebsiteGenerator.Entities;
using System.Data;
using MobileWebsiteGenerator.Mailers;

namespace MobileWebsiteGenerator.Controllers
{
    public class WebsiteController : HomeController
    {
        //
        // GET: /WebSite/Website/
        public Mobile_appContext context = new Mobile_appContext();
        public ActionResult Create()
        {
            return View(MvcApplication._MainCustomer);
        }

        [HttpPost]
        public ActionResult Create(MainViewModel mainviewModel)
        {


            char[] obj = new char[] { ',' };
            if (MvcApplication.IsUpdateMode == false || MvcApplication.IscreateNewFront == true)
            {
                //Create Customer and if already exist then create new website.

                //Write code for update all data 

                //add Website           
                var website = new Website();
                //var pictureFlag = Request["Website.IsPictureGallery"].Split(obj);
                //if (pictureFlag.Count() == 1)
                website.IsPictureGallery = Convert.ToBoolean(Request["pictureTemp"]);
                //else
                //    website.IsPictureGallery = Convert.ToBoolean(pictureFlag[1]);
                //  website.IsPictureGallery = Convert.ToBoolean(Request["Website.IsPictureGallery"]);
                var contact = Request["Website.IsContactusPage"].Split(obj);
                if (contact.Count() == 1)
                    website.IsContactusPage = Convert.ToBoolean(Request["Website.IsContactusPage"]);
                else
                    website.IsContactusPage = Convert.ToBoolean(contact[1]);
                website.visitorcount = MvcApplication._MainCustomer.Website.visitorcount;
                website.web_url = Request["Website_web_url"];
                string hashCode = String.Format("{0:X}", website.web_url.GetHashCode());
                website.web_url = "http://mwg.com/" + MvcApplication._MainCustomer.customer.UserName + "/" + website.web_url;
                context.Websites.Add(website);
                context.SaveChanges();
                //Customer Website rel


                //Add Customer

                var custwebsite = new CustomerWebsiteREL();
                custwebsite.Customer_id = MvcApplication._MainCustomer.customer.Customer_id;
                custwebsite.Web_id = website.web_id;
                custwebsite.Customer_Web_id = Convert.ToDecimal((context.CustomerWebsiteRels == null || context.CustomerWebsiteRels.Count() == 0) ? 1 : context.CustomerWebsiteRels.Count() + 1);
                context.CustomerWebsiteRels.Add(custwebsite);
                context.SaveChanges();
                //Add theme
                var myTheme = new ThemeDetail();
                myTheme.Headercolor = MvcApplication._MainCustomer.MyTheme.Headercolor;
                myTheme.HeadertextColor = MvcApplication._MainCustomer.MyTheme.HeadertextColor;

                myTheme.Footertext = MvcApplication._MainCustomer.MyTheme.Footertext;

                myTheme.FootertextColor = MvcApplication._MainCustomer.MyTheme.FootertextColor;
                myTheme.Border = MvcApplication._MainCustomer.MyTheme.Border;
                myTheme.Size = MvcApplication._MainCustomer.MyTheme.Size;
                myTheme.ButtonPicture = MvcApplication._MainCustomer.MyTheme.ButtonPicture;


                myTheme.BannerImage = MvcApplication._MainCustomer.MyTheme.BannerImage;
                myTheme.Bannertext = MvcApplication._MainCustomer.MyTheme.Bannertext;


                myTheme.HeaderImage = MvcApplication._MainCustomer.MyTheme.HeaderImage;
                myTheme.HeaderText = MvcApplication._MainCustomer.MyTheme.HeaderText;
                myTheme.Image = MvcApplication._MainCustomer.MyTheme.Image;
                myTheme.Swatch = MvcApplication._MainCustomer.MyTheme.Swatch;
                myTheme.HHeaderText = MvcApplication._MainCustomer.MyTheme.HHeaderText;
                myTheme.HFootertext = MvcApplication._MainCustomer.MyTheme.HFootertext;

                myTheme.Theme_type = MvcApplication._MainCustomer.MyTheme.Theme_type;
                myTheme.backgroundColor = MvcApplication._MainCustomer.MyTheme.backgroundColor;
                myTheme.backgroundImage = MvcApplication._MainCustomer.MyTheme.backgroundImage;


                myTheme.IsFooter = MvcApplication._MainCustomer.MyTheme.IsFooter;
                myTheme.Isheader = MvcApplication._MainCustomer.MyTheme.Isheader;
                myTheme.facebookurl = MvcApplication._MainCustomer.MyTheme.facebookurl;
                myTheme.twitterurl = MvcApplication._MainCustomer.MyTheme.twitterurl;
                myTheme.gplusurl = MvcApplication._MainCustomer.MyTheme.gplusurl;



                context.ThemeDetails.Add(myTheme);
                context.SaveChanges();


                //Theme web rel

                WebthemeRel webthemerel = new WebthemeRel();
                webthemerel.Theme_id = myTheme.theme_id;
                webthemerel.Web_id = website.web_id;
                webthemerel.Web_theme_id = Convert.ToDecimal((context.WebthemeRels == null || context.WebthemeRels.Count() == 0) ? 1 : context.WebthemeRels.Select(wt => wt.Web_theme_id).Max() + 1);
                context.WebthemeRels.Add(webthemerel);






                //Add Picture Gallery
                if (MvcApplication._MainCustomer.Website.IsPictureGallery == true)
                {

                    MvcApplication._MainCustomer.PictureGallery.ForEach(p =>
                    {
                        //Add Picture
                        var piture = new pictureGallery();
                        piture.ImagePath = p.ImagePath;
                        piture.Size = p.Size;
                        context.pictureGalleries.Add(piture);
                        context.SaveChanges();

                        //Add pictureRelWith Theme
                        ThemePictureREL thempic = new ThemePictureREL();
                        thempic.Theme_id = myTheme.theme_id;
                        thempic.Picture_id = piture.picture_id;
                        thempic.Theme_picture_id = Convert.ToDecimal((context.ThemePictureRELs == null || context.ThemePictureRELs.Count() == 0) ? 1 : context.ThemePictureRELs.Select(tp => tp.Theme_picture_id).Max() + 1);
                        context.ThemePictureRELs.Add(thempic);
                        context.SaveChanges();
                    });
                }


                //Add Pages

                MvcApplication._MainCustomer.ContentPages.ForEach(p =>
                {
                    //Add Picture
                    var page = new Page();

                 
                    page.EContent = p.EContent;
                    page.title = p.title;
                    page.HContent = p.HContent;
                    page.HTitle = p.HTitle;
                    page.Priority = p.Priority;
                    context.Pages.Add(page);
                    context.SaveChanges();

                    //Add pictureRelWith Theme
                    ThemePageREL thempic = new ThemePageREL();
                    thempic.Theme_id = myTheme.theme_id;
                    thempic.Page_id = page.page_id;
                    thempic.Theme_Page_id = Convert.ToDecimal((context.ThemePageRELs == null || context.ThemePageRELs.Count() == 0) ? 1 : context.ThemePageRELs.Select(tp => tp.Theme_Page_id).Max() + 1);

                    context.ThemePageRELs.Add(thempic);
                    context.SaveChanges();
                });

            }
            else
            {

                var website = context.Websites.Where(web => web.web_id == context.CustomerWebsiteRels.Where(w => w.Customer_id == MvcApplication._MainCustomer.customer.Customer_id).Select(p => p.Web_id).FirstOrDefault()).FirstOrDefault();

                website.web_url = Request["Website_web_url"];

                website.IsPictureGallery = Convert.ToBoolean(Request["pictureTemp"]);
                //if (pictureFlag.Count() == 1)
                //    website.IsPictureGallery = Convert.ToBoolean(Request["Website.IsPictureGallery"]);
                //else
                //    website.IsPictureGallery = Convert.ToBoolean(pictureFlag[1]);

                var contact = Request["Website.IsContactusPage"].Split(obj);
                if (contact.Count() == 1)
                    website.IsContactusPage = Convert.ToBoolean(Request["Website.IsContactusPage"]);
                else
                    website.IsContactusPage = Convert.ToBoolean(contact[1]);
                context.SaveChanges();



                int themeid = context.WebthemeRels.Where(w => w.Web_id == website.web_id).Select(p => p.Theme_id).FirstOrDefault();
                var theme = context.ThemeDetails.Where(t => t.theme_id == themeid).FirstOrDefault();
                theme.Swatch = MvcApplication._MainCustomer.MyTheme.Swatch;
                theme.backgroundColor = MvcApplication._MainCustomer.MyTheme.backgroundColor;

                theme.HeadertextColor = MvcApplication._MainCustomer.MyTheme.HeadertextColor;
                theme.FootertextColor = MvcApplication._MainCustomer.MyTheme.FootertextColor;
                theme.Size = MvcApplication._MainCustomer.MyTheme.Size;
                theme.Border = MvcApplication._MainCustomer.MyTheme.Border;
                theme.ButtonPicture = MvcApplication._MainCustomer.MyTheme.ButtonPicture;


                theme.BannerImage = MvcApplication._MainCustomer.MyTheme.BannerImage;
                theme.Bannertext = MvcApplication._MainCustomer.MyTheme.Bannertext;



             


                theme.Footertext = MvcApplication._MainCustomer.MyTheme.Footertext;
                theme.Headercolor = MvcApplication._MainCustomer.MyTheme.Headercolor;
                theme.HeaderText = MvcApplication._MainCustomer.MyTheme.HeaderText;
                theme.HHeaderText = MvcApplication._MainCustomer.MyTheme.HHeaderText;
                theme.HFootertext = MvcApplication._MainCustomer.MyTheme.HFootertext;
                theme.Image = MvcApplication._MainCustomer.MyTheme.Image;
                theme.Swatch = MvcApplication._MainCustomer.MyTheme.Swatch;
                theme.Theme_type = MvcApplication._MainCustomer.MyTheme.Theme_type;
                theme.facebookurl = MvcApplication._MainCustomer.MyTheme.facebookurl;
                theme.twitterurl = MvcApplication._MainCustomer.MyTheme.twitterurl;
                theme.gplusurl = MvcApplication._MainCustomer.MyTheme.gplusurl;
                theme.IsFooter = MvcApplication._MainCustomer.MyTheme.IsFooter;
                theme.Isheader = MvcApplication._MainCustomer.MyTheme.Isheader;
                context.SaveChanges();

                var pages = context.Pages.Where(page => context.ThemePageRELs.Where(p => p.Theme_id == themeid).Select(p => p.Page_id).Contains(page.page_id));

                if (pages != null)
                {

                    pages.ToList().ForEach(p =>
                    {
                        context.ThemePageRELs.Remove(context.ThemePageRELs.Where(tp => tp.Page_id == p.page_id).FirstOrDefault());
                        context.Pages.Remove(p);
                        context.SaveChanges();
                    });

                    MvcApplication._MainCustomer.ContentPages.ForEach(p =>
                    {
                        //Add Picture
                        var page = new Page();
                        page.EContent = p.EContent;
                        page.title = p.title;
                        page.HContent = p.HContent;
                        page.HTitle = p.HTitle;
                        page.Priority = p.Priority;
                        context.Pages.Add(page);
                        context.SaveChanges();

                        //Add pictureRelWith Theme
                        ThemePageREL thempic = new ThemePageREL();
                        thempic.Theme_id = themeid;
                        thempic.Page_id = page.page_id;
                        thempic.Theme_Page_id = Convert.ToDecimal((context.ThemePageRELs == null || context.ThemePageRELs.Count() == 0) ? 1 : context.ThemePageRELs.Select(tpr => tpr.Theme_Page_id).Max() + 1);

                        context.ThemePageRELs.Add(thempic);
                        context.SaveChanges();
                    });

                }



                var pictures = context.pictureGalleries.Where(page => context.ThemePictureRELs.Where(p => p.Theme_id == themeid).Select(p => p.Picture_id).Contains(page.picture_id));

                if (pictures != null)
                {
                    pictures.ToList().ForEach(p =>
                    {
                        context.ThemePictureRELs.Remove(context.ThemePictureRELs.Where(tp => tp.Picture_id == p.picture_id).FirstOrDefault());
                        context.pictureGalleries.Remove(p);
                        context.SaveChanges();
                    });
                    if (website.IsPictureGallery == true)
                    {
                        MvcApplication._MainCustomer.PictureGallery.ForEach(p =>
                        {
                            //Add Picture
                            var piture = new pictureGallery();
                            piture.ImagePath = p.ImagePath;
                            piture.Caption = p.Caption;
                            piture.Size = p.Size;
                            context.pictureGalleries.Add(piture);
                            context.SaveChanges();

                            //Add pictureRelWith Theme
                            ThemePictureREL thempic = new ThemePictureREL();
                            thempic.Theme_id = themeid;
                            thempic.Picture_id = piture.picture_id;
                            thempic.Theme_picture_id = Convert.ToDecimal((context.ThemePictureRELs == null || context.ThemePictureRELs.Count() == 0) ? 1 : context.ThemePictureRELs.Select(tpi => tpi.Theme_picture_id).Max() + 1);
                            context.ThemePictureRELs.Add(thempic);
                            context.SaveChanges();
                        });


                    }
                }






            }






            return RedirectToActionPermanent("LastStep", "Home");
        }


        public ActionResult Metalist(MainViewModel mainviewModel)

        {
         //   MvcApplication._MainCustomer.metatags = context.Metatags.Where(cm => cm.MetaID == ID).ToList();
            return View();
        }



        public ActionResult publish()
        {
            //MvcApplication._MainCustomer.customer = new Customer();
            //MvcApplication._MainCustomer.ContentPages = new List<Page>();
            //MvcApplication._MainCustomer.MyTheme = new ThemeDetail();
            //MvcApplication._MainCustomer.PictureGallery = new List<pictureGallery>();
            //MvcApplication._MainCustomer.Website = new Website();
            //Get Customer ID from Query String
            if (MvcApplication._MainCustomer.Website.web_id != 1)
            {
                GetDataFromDB.GetAllData(MvcApplication._MainCustomer.customer.Customer_id);
            }
            else
            {
                MvcApplication.showMessage = true;
                MvcApplication.messageTxt = "First Create The website ";
                return RedirectToAction("create", "Website");
            }

            return View(MvcApplication._MainCustomer);

        }


        public ActionResult AboutPages(int id)
        {
            var page = MvcApplication._MainCustomer.ContentPages.Where(p => p.page_id == id).FirstOrDefault();
            return View(page);
        }

        public ActionResult PictureGallery()
        {
            //var picture = MvcApplication._MainCustomer.PictureGallery.Where(pic => pic.picture_id == id).FirstOrDefault();
            return View(MvcApplication._MainCustomer.PictureGallery);
        }
        public ActionResult ContactUs()
        {

            return View();
        }

        //[HttpPost]
        //public ActionResult ContactUs(ContactUs contactdata)
        //{

        //    UserMailer.Welcome(contactdata).Send();
        //    return RedirectToAction("MailSent");
        //}

        public ActionResult MailSent()
        {
            return View();
        }



        public ActionResult MetaTag()
        {
            return View(MvcApplication._MainCustomer);
        }

        [HttpPost]
        public ActionResult MetaTag(MainViewModel MetaTags)
        {

            if (ModelState.IsValid)
            {


                var metatags = new Metatag();
               // metatags.MetaID = MvcApplication._MainCustomer.metatags.MetaID;
                metatags.KeyWords = Request["KeyWords"];
                metatags.Description = Request["Description"];
                context.Metatags.Add(metatags);
                context.SaveChanges();

                var webmetarel = new WebMetaREL();
                webmetarel.Meta_ID = metatags.MetaID;
                webmetarel.Web_id = MvcApplication._MainCustomer.Website.web_id;
                webmetarel.Web_Meta_id = Convert.ToDecimal((context.WebMetaRELs == null || context.WebMetaRELs.Count() == 0) ? 1 : context.WebMetaRELs.Count() + 1);
                context.WebMetaRELs.Add(webmetarel);
                context.SaveChanges();
              
                return RedirectToAction("Create", "Website");


            }
            return View(MvcApplication._MainCustomer);

        }


        public ActionResult EditMetaTag(int id)
        {
            var metatags = context.WebMetaRELs.Where(m => m.Web_id == MvcApplication._MainCustomer.Website.web_id).FirstOrDefault();
            if (metatags != null)
            {
                var mymetatags = context.Metatags.Where(m => m.MetaID == metatags.Meta_ID).FirstOrDefault();

                return View(mymetatags);
            }
            else
            {
                return RedirectToAction("MetaTag", "Website");
            }
            return View();
        }

        [HttpPost]
        public ActionResult EditMetaTag(Metatag MetaTags)
        {

            if (ModelState.IsValid)
            {


                context.Entry(MetaTags).State = EntityState.Modified;
                context.SaveChanges();



                return RedirectToAction("Create", "Website");


            }
            return View(MvcApplication._MainCustomer);

        }

       



    


    }
}
